package com.qk.management.task;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONUtil;
import com.qk.doman.clue.BusinessCountDO;
import com.qk.doman.clue.ClueCountDO;
import com.qk.entity.OverviewVO;
import com.qk.management.aop.RedisAop;
import com.qk.management.mapper.BusinessMapper;
import com.qk.management.mapper.ClueMapper;
import com.qk.management.service.Impl.ReportOverviewServiceImpl;
import com.qk.management.service.ReportOverviewService;
import jakarta.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author 16778
 * @date 2025/10/18 14:34
 * @description
 */
@Component
public class RedisTask {
    private static final String CACHE_PORTAL_KEY_PREFIX = "qk:";
    @Autowired
    private ClueMapper clueMapper;

    @Autowired
    private BusinessMapper businessMapper;

    @Autowired
    private StringRedisTemplate redisClient;

    @Scheduled(cron = "0 0 * * * ?")
    public void update(){
        System.out.println("开始更新redis缓存");

        try {
            ClueCountDO clueCountDO = clueMapper.count();
            BusinessCountDO businessCountDO = businessMapper.count();
            OverviewVO vo = BeanUtil.copyProperties(clueCountDO, OverviewVO.class);
            BeanUtil.copyProperties(businessCountDO, vo);
            String redisKey = CACHE_PORTAL_KEY_PREFIX + "reportOverview";
            redisClient.opsForValue().set(redisKey, JSONUtil.toJsonStr(vo));
            System.out.println("redis缓存更新完成");
        } catch (Exception e) {
            System.err.println("redis更新错误: " + e.getMessage());
            e.printStackTrace();
        }
    }
}
